﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FrameLineItem : MonoBehaviour, INormalItem
{
    public Text frameNum;
    public UGUIToggle select;
    public Button intteruptBtn, eventBtn, atkBtn;

    private FrameInfo data;

    private void OnEnable()
    {
        EventManager.AddEventListener<FrameInfo>(CommonEvent.PlayingUpdateFrameLine, OnPlayingUpdateFrameLine);
    }
    private void OnDisable()
    {
        EventManager.RemoveEventListener<FrameInfo>(CommonEvent.PlayingUpdateFrameLine, OnPlayingUpdateFrameLine);
    }

    private void OnPlayingUpdateFrameLine(FrameInfo info)
    {
        bool bo = info.FrameIndex == data.FrameIndex;
        select.ChangeForce(bo);
    }
    public void SetData(FrameInfo info)
    {
        data = info;
        select.onValueChangedEvent = OnSelectToggle;
        
        frameNum.text = data.FrameIndex.ToString();
        SetToggleState(data);
    }
    private void SetToggleState(FrameInfo info)
    {
        intteruptBtn.gameObject.SetActive(info.interrupt != null);
        eventBtn.gameObject.SetActive(info.mEvent != null);
        atkBtn.gameObject.SetActive(info.attackDef != null);
    }

    private void OnSelectToggle(object obj, bool value)
    {
        if (value == true )
        {
            EventManager.TriggerEvent<FrameInfo>(CommonEvent.Select_One_FrameLine, data);
        }
    }
    public void OnIntteruptClick()
    {
        EventManager.TriggerEvent<Data.ActionInterrupt>(CommonEvent.FrameItemLineSelectInterrupt, data.interrupt);
    }
    public void OnEventClick()
    {
        EventManager.TriggerEvent<Data.Event>(CommonEvent.FrameItemLineSelectEvent, data.mEvent);
    }
    public void OnAttackDefClick()
    {
        EventManager.TriggerEvent<Data.AttackDef>(CommonEvent.FrameItemLineSelectAttackDef, data.attackDef);
    }

    public void OnRecycle()
    {
        select.isOn = false;
        select.onValueChangedEvent = null;
    }
}
